package 力扣日常刷题.木22一月.第14天0123;

/**
 * @author 帅小伙
 * @date 2022/1/23
 * @description
 * https://leetcode-cn.com/problems/filling-bookcase-shelves/solution/java-dong-tai-gui-hua-jie-fa-by-dan-wei-te7vz/
 */
public class Demo58动态规划填充书架 {


    public int minHeightShelves(int[][] books, int shelfWidth) {
        int n = books.length;
        // dp[i]  前n本书最小高度
        int[] dp = new int[n+1];
        dp[1] = books[0][1];

        for (int i = 2; i <= n ; i++) {
            dp[i] = books[i-1][1] + dp[i-1];
            int w = books[i-1][0],h = books[i-1][1];
            for (int j = i -1 ; j >0;j--){
                w += books[j-1][0];
                if(w > shelfWidth) break;
                h = Math.max(h,books[j-1][1]);
                dp[i] = Math.min(dp[i],dp[j-1]+h);
            }
        }
        return dp[n];
    }

}
